/**
 * @Controller 块
 */
const Controller = require('egg').Controller;

class BlocksController extends Controller {

    async show() {
        const ctx = this.ctx;
        try {
            const data = await ctx.service.block.showBlock(ctx.params.id)
            ctx.setRes.success(data);
        }
        catch (err) {
            throw err;
        }
    }

    async showFreeBlock() {
        const ctx = this.ctx;
        try {
            const data = await ctx.service.block.showFreeBlock(ctx.params.id)
            ctx.setRes.success(data);
        }
        catch (err) {
            throw err;
        }
    }

    async index() {
        const ctx = this.ctx;
        try {
            const data = await ctx.service.block.getBlocks(ctx.query)
            ctx.body = {
                code: 200,
                data,
                success: true,
                msg: ``
            };
        }
        catch (err) {
            throw err;
        }
    }

    async create() {
        const { ctx } = this;
        try {
            const data = await ctx.service.block.createBlock(ctx.request.body)
            ctx.body = {
                code: 200,
                data: {
                    id: data._id
                },
                success: true,
                msg: '',
            }
        } catch (error) {
            throw error;
        }
    }

    async destroy() {
        const { ctx } = this;
        try {
            const result = await ctx.service.block.deleteBlock(ctx.params.id);
            if (result && result._id) {
                ctx.body = {
                    code: 200,
                    data: {},
                    success: true,
                    msg: '删除成功'
                }
            } else {
                throw new Error('删除失败')
            }
        } catch (error) {
            throw error;
        }
    }

    async edit() {
        const { ctx } = this;
        try {
            const result = await ctx.service.block.editBlock(ctx.params.id);
            ctx.body = {
                code: 200,
                data: result,
                success: true,
                msg: ''
            }
        } catch (error) {
            throw error;
        }
    }

    async update() {
        const { ctx } = this;
        try {
            const result = await ctx.service.block.updateBlock(ctx.params.id, ctx.request.body);
            ctx.body = {
                code: 200,
                data: result,
                success: true,
                msg: ''
            }
        } catch (error) {
            throw error;
        }
    }

    async publish() {
        const { ctx } = this;
        try {
            const result = await ctx.service.block.publishBlock(ctx.params.id);
            ctx.body = {
                code: 200,
                data: result,
                success: true,
                msg: ''
            }
        } catch (error) {
            throw error;
        }
    }

    async unlock() {
        const { ctx } = this;
        try {
            const result = await ctx.service.block.unlockBlock(ctx.params.id);
            ctx.body = {
                code: 200,
                data: result,
                success: true,
                msg: ''
            }
        } catch (error) {
            throw error;
        }
    }
}

module.exports = BlocksController;
